package cn.com.surker.material.controller;

import cn.com.surker.common.AbstractController;
import cn.com.surker.material.entity.MainMaterial;
import cn.com.surker.material.service.MainMaterialService;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lucky
 * @create 2024年07月04 8:22
 * @desc 主材信息控制类
 */
@RestController
@RequestMapping("/main-material")
public class MainMaterialController extends AbstractController {

    @Resource
    private MainMaterialService materialService;

    /**
     * 分页查询材料信息
     * @param searchVal 检索条件
     * @param status 状态(0:启用1：禁用)
     * @param type 类型 材料大类
     * @param page
     * @param limit
     * @param curr
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResultDto page(String searchVal, Integer status, Integer type, Long orgId, Long materialId, Integer isDisPlay,
                              Integer storeType, Integer page, Integer limit, Integer curr){
        try {
            /**分页对象**/
            if (!StringUtils.isNull(curr))//layui不能用page传页码数则修改默认页码参数传入
                page = curr;
            else {
                if (StringUtils.isNull(page))
                    page = 1;
            }
            if (StringUtils.isNull(limit))
                limit = 15;
            SysUser user = getUser();
            if (user == null || user.getStatus() == 1)
                return RestResultDto.fail("登录信息失效,请重新登录");

            IPage iPage = materialService.findPage(searchVal, status,
                    type, orgId,  materialId, isDisPlay, storeType, (page - 1) * limit, limit);
            return RestResultDto.success(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,请稍后再试");
        }
    }
    @RequestMapping(value = "/find-supplier", method = RequestMethod.POST)
    public RestResultDto findBySupplierMaterial(Long orgId, Long supplierId){
        try {
            List<MainMaterial> list = materialService.findSupplierMaterial(orgId, supplierId);
            if (list == null || list.size() == 0)
                return RestResultDto.fail("未获取到供应商材料数据");
            return RestResultDto.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,请稍后再试");
        }
    }

    /**
     * 新增材料
     * @param materialId 材料ID
     * @param orgId 公司ID
     * @param name 材料名称
     * @param norms 规格
     * @param nameNorms 名称规格
     * @param parentId 父级ID
     * @param storeType 仓位类型(0:无1:筒仓2:堆场)
     * @param remark
     * @param isDisplay
     * @param code
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    public RestResultDto save(Long materialId, Long orgId,String name, String norms, String nameNorms, Long parentId,
                              Integer storeType, String remark, Integer isDisplay, String code, Integer type, Integer breedType, Integer weightBillType){

        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");

            Integer resultCode = materialService.save(materialId, orgId, name, norms, nameNorms, parentId, storeType,
                    remark, isDisplay, sysUser.getName(), code, type, breedType, weightBillType);
            return RestResultDto.complete(resultCode);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }

    /**
     *
     * @param id
     * @param materialId 材料ID
     * @param orgId 公司ID
     * @param name 材料名称
     * @param norms 规格
     * @param nameNorms 名称规格
     * @param parentId 父级ID
     * @param storeType 仓位类型(0:无1:筒仓2:堆场)
     * @param remark
     * @param isDisplay
     * @param code
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public RestResultDto update(@PathVariable Long id, Long materialId, Long orgId, String name, String norms, String nameNorms, Long parentId,
                                Integer storeType, String remark, Integer isDisplay, String code, Integer type, Integer breedType, Integer weightBillType){

        try{
            CacheUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");
            Integer isAdmin = 0;
            if (sysUser.getRoleNameList().contains("系统管理员") ||sysUser.getRoleNameList().contains("软件工程师"))
                isAdmin = 1;
            Integer resultCode = materialService.update(id, materialId, orgId, name, norms, nameNorms, parentId, storeType,
                    remark, isDisplay, sysUser.getName(), code, type, breedType, weightBillType, isAdmin);
            return RestResultDto.complete(resultCode);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }

    /**
     * 修改材料状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "/update-status/{id}", method = RequestMethod.PUT)
    public RestResultDto updateStatus(@PathVariable Long id, Integer status){

        try{
            SysUser sysUser = getUser();
            if (sysUser == null)
                return RestResultDto.fail("登录信息已失效,请重新登录");
            if (sysUser.getStatus() == 1)
                return RestResultDto.fail("账号已禁用,请联系管理人员");

            Integer resultCode = materialService.updateStatus(id, status);
            return RestResultDto.complete(resultCode);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("保存失败,请稍后再试");
        }
    }
}
